import os
import subprocess
import sys

# 配置文件路径
PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# 默认配置路径（Windows环境）
DEVECO_DIR = r"D:\huawei\command-line-tools"
node_path = os.path.join(DEVECO_DIR, r"tool\node\node.exe")
npm_path = os.path.join(DEVECO_DIR, r"tool\node\npm.cmd")
EXCEL_FILE_PATH = os.path.join(PROJECT_DIR, "data", "三方库测试表.xlsx")

# 配置路径
GIT_PATH = r"C:\Program Files\Git\git-bash.exe"

os.environ["PYTHONUNBUFFERED"] = "1"  # 防止终端输出乱码
os.environ['PYTHONIOENCODING'] = 'utf-8'
os.environ['DEVECO_SDK_HOME'] = r'D:\huawei\command-line-tools\sdk'
os.environ['OHOS_BASE_SDK_HOME'] = r'D:\huawei\SDK\openharmony'
hvigor_path = os.path.join(DEVECO_DIR, r"hvigor\bin\hvigorw.js")
ohpm_path = os.path.join(DEVECO_DIR, r"ohpm\bin\ohpm.bat")

def check_dependencies():
    """检查必要的依赖项是否安装"""
    required = ['git', 'ohpm']
    for dep in required:
        try:
            subprocess.run([dep, '--version'], check=True,
                           stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
        except subprocess.CalledProcessError:
            print(f"错误: {dep} 未正确安装或版本检查失败!")
            sys.exit(1)
        except FileNotFoundError:
            print(f"错误: 找不到{dep}, 请确认已安装并添加到PATH环境变量!")
            sys.exit(1)


# SDK版本与API版本的映射关系
SDK_API_MAPPING = {
    "5.0.0": "12",
    "5.0.1": "13",
    "5.0.2": "14",
    "5.0.3": "15",
    "5.0.4": "16",
    "5.0.5": "17",
    "5.1.0": "18",
    "5.1.1": "19",
    "6.0.0": "20"
}

# 全局变量存储用户选择的SDK版本
selected_sdk_version = None
selected_api_version = None

def set_sdk_version(sdk_version):
    """设置SDK版本和对应的API版本"""
    global selected_sdk_version, selected_api_version
    selected_sdk_version = sdk_version
    selected_api_version = get_api_version(sdk_version)

def get_api_version(sdk_version):
    """根据SDK版本获取对应的API版本号"""
    if sdk_version in SDK_API_MAPPING:
        return SDK_API_MAPPING[sdk_version]
    # 如果找不到对应版本，返回最新版本
    print(f"警告：未找到SDK版本 {sdk_version} 对应的API版本，将使用最新版本")
    return list(SDK_API_MAPPING.values())[-1]